<?php

namespace Web\MainSiteBundle\Controller;

use BaseExt\Pagination;
use Model\CommentsBundle\Entity\Comments;

use Model\CommentsBundle\Model\CommentsModel;
use Model\CommentsReplyBundle\Model\CommentsReplyModel;
use Model\CourseBundle\Entity\Course;
use Model\CourseCategoryBundle\Model\CourseCategoryModel;
use Model\ExamBundle\Entity\Exam;
use Model\ExamBundle\Entity\ExamStatic;
use Model\ExamBundle\Model\ExamModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * @route("Exam")
 */
class ExamController extends WebBaseController
{
    /**
     * @route("/add",name="WebExamAdd")
     */
    public function WebExamAdd(Request $request)
    {
        $user = $this->needToBeTeacher();

        if ($request->getMethod() == "POST") {
            if (!$type = $this->getParamRequired("type")) {
                if ($type !== "0") {
                    return $this->msg_out("请选择题型");
                }
            }
            if (!$title = $this->getParamRequired("title")) {
                return $this->msg_out("请填写题干");
            }
            if (!$Point = $this->getParamRequired("Point")) {
                return $this->msg_out("请设置知识点");
            }
            if (!$totalScore = $this->getParamRequired("totalScore")) {
                return $this->msg_out("请设置总分");
            }
            if (!$totalTime = $this->getParamRequired("totalTime")) {
                return $this->msg_out("请设置考试时间");
            }
            if (!$intro = $this->getParamRequired("intro")) {
                return $this->msg_out("请设置考试要求");
            }
            $em = $this->getDoctrine()->getManager();
            if ($id = $this->getParamRequired("id")) {
                //传入id则是更新，否则就新建
                $rep = $this->getDoctrine()->getRepository("ModelExamBundle:Exam");
                $exam = $rep->find($id);
                $msg = "信息已修改";
            } else {
                $exam = new Exam();
                $msg = "添加成功";
            }
            $exam->setType($type);
            $exam->setSort($Point);
            $exam->setTitle($title);
            $exam->setIntro($intro);
            $exam->setTotalScore($totalScore);
            $exam->setTotalTime($totalTime);
            $exam->setUserId($user->getId());

            $em->persist($exam);
            $em->flush();

            $examStatic = new ExamStatic();
            $examStatic->setExamId($exam->getId());

            $em->persist($examStatic);
            $em->flush();
            return $this->msg_out("$msg",$this->generateUrl("WebExamMy"));

        } else {
            $CourseCategory = new CourseCategoryModel($this->getDoctrine());
            $groups = $CourseCategory->getAllGroupByParent();
            $data = array(
                "sorts" => json_encode($groups),
            );

            //如果传入id则尝试获取数据进行修改
            if ($id = $this->getParamRequired("id")) {
                $ExamModel = new ExamModel($this->getDoctrine());
                $Exam = $ExamModel->getExamOne($id);
                $rep = $this->getDoctrine()->getRepository("ModelCourseCategoryBundle:CourseCategory");
                $Sort = $rep->find($Exam["sort"]);
                $link = $Sort->getLink();
                $link = str_replace("]","",$link);
                $linkArr = explode("[",substr($link,1));

                $data["Exam"] = $Exam;
                $data["Sort"] = json_encode($linkArr);

            }


            return $this->render("WebMainSiteBundle:Exam:exam_add.html.twig",$data);
        }


    }

    /**
     * @route("/my",name="WebExamMy")
     */
    public function WebExamMy(Request $request)
    {
        $user = $this->needToBeTeacher();
        $userId = $user->getId();
        $examModel = new ExamModel($this->getDoctrine());
        $exam = $examModel->getByUserId($userId);

        $data = array(
            "exam" => $exam,
        );
        return $this->render("WebMainSiteBundle:Exam:exam_list.html.twig",$data);

    }


    /**
     * @route("Question/Edit/{id}",name="WebExamQuestionEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function WebExamQuestionEdit(Exam $Exam)
    {
        $data = array(
            "Exam" => $Exam,
        );
        return $this->render("WebMainSiteBundle:Exam:exam_question_edit.html.twig",$data);
    }

}
